﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;

using System.Data;
using System.Data.Common;
using System.Data.Sql;


namespace ProjectSem3
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        private dataDataContext dc = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    // hiển thị my acuont
                    if (Session["user"] != null)
                    {
                        LabelnameSigin.Text = ((Customer)Session["user"]).Name;
                        LabelshowUsername.Text = ((Customer)Session["user"]).Username;
                        LabelshowBirthday.Text = ((Customer)Session["user"]).BirthDay.ToString();
                        Labelshowgender.Text = ((Customer)Session["user"]).Gender.ToString();
                        Labelshowemail.Text = ((Customer)Session["user"]).Email;
                        Labelshowaddress.Text = ((Customer)Session["user"]).Address;
                        Labelshowcompanyaddress.Text = ((Customer)Session["user"]).CompanyAddress;
                        Labelshowphone.Text = ((Customer)Session["user"]).Phone;
                    }
                    this.Repeater_Product.DataSource = this.dc.Products.ToList();
                    this.Repeater_Product.DataBind();
                }
                catch (Exception)
                { }



                // hiển thị bãng order
                this.hienthidulieu();

            }

        }







        protected void ButtonsaveUsername_Click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.Username = this.TextBoxUsername.Text;
            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsavePassword_Click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();

            string passOld = FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBoxCheckpassOld.Text, "sha1");
            if (cus.Password.Equals(passOld))
            {
                cus.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBoxPassword.Text, "sha1");
                this.dc.SubmitChanges();
                Session.Remove("user");
                Response.Redirect("Default.aspx");
            }
            else
            {
                LabelshowPassword.Text = "lỗi mẹ nó rồi";
            }

        }
        protected void Buttonsavename_click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.Name = this.TextBoxname.Text;

            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsaveBirthday_click(object sender, EventArgs e)
        {

            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.BirthDay = Convert.ToDateTime(this.TextBoxBirthday.Text);
            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsaveGender_click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            foreach (ListItem l in RadioButtonListGender.Items)
            {

                if (l.Selected)
                {
                    if (l.Text.Equals("Male"))
                        cus.Gender = true;
                    else
                        cus.Gender = false;
                }
            }

            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsaveEmail_Click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.Email = this.TextBoxEmail.Text;
            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsaveAddress_Click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.Address = this.TextBoxAddress.Text;
            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsaveCompanyAddress_Click(object sender, EventArgs e)
        {
            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.CompanyAddress = this.TextBoxCompanyAddress.Text;
            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void ButtonsavePhone_Click(object sender, EventArgs e)
        {

            int khachhang = ((Customer)(Session["user"])).CustomerID;
            Customer cus = this.dc.Customers.Where(c => c.CustomerID == khachhang).FirstOrDefault();
            cus.Phone = this.TextBoxPhone.Text;
            this.dc.SubmitChanges();
            Session["user"] = cus;
            Response.Redirect("MyAccount.aspx");
        }
        protected void Repeater_Product_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField productID = e.Item.FindControl("productID") as HiddenField;
            var img = this.dc.ImagesPros.Where(i => i.ProductID == Int32.Parse(productID.Value)).Skip(0).Take(1);
            Image image = e.Item.FindControl("image") as Image;
            foreach (var a in img)
            {
                image.ImageUrl = "~/images/Product/" + a.NameImg;
            }
        }









        // phần order
        protected void ListViewOrder_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (Session["user"] != null)
            {

                Label lbl = e.Item.FindControl("labelOrderID") as Label;
            }
 
        }



        private void hienthidulieu()
        {
            //// code chuẫn.
            int cusid = ((Customer)(Session["user"])).CustomerID;        
            var data = dc.Orders.Where(h=>h.CustomerID==cusid).OrderByDescending(d => d.DateOrder).ToList(); 
            this.ListViewOrder.DataSource = data;
            this.ListViewOrder.DataBind();
        }
    }
}